<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Servers extends Admin_Controller {
	public function __construct()
	{
		parent::Admin_Controller();
		$this->load->helper('form');
		$this->lang->load('cirpho');
		$this->lang->load('servers');
		$this->load->model('cip_servers_m');
		$this->template
			->append_metadata( css('cirpho.css', 'cirpho') )
			->set_partial('shortcuts', 'admin/partials/shortcuts');
		// Set the validation rules
		$this->load->library('form_validation');
		$this->validation_rules = array(
			array(
				'field' => 'name',
				'label' => lang('server.title_label'),
				'rules' => 'trim|required|max_length[255]'
			),
			array(
				'field' => 'domain_name',
				'label' => lang('server.domain_name_label'),
				'rules' => 'trim|required|max_length[255]'
			),
			array(
				'field' => 'database_user',
				'label' => lang('server.database_user_label'),
				'rules' => 'trim|required|max_length[255]'
			),
			array(
				'field' => 'database_name',
				'label' => lang('server.database_name_label'),
				'rules' => 'trim|required|max_length[255]'
			),
			array(
				'field' => 'database_password',
				'label' => lang('server.database_password_label'),
				'rules' => 'trim|required|max_length[255]'
			),
		);
		$this->form_validation->set_rules($this->validation_rules);
	}
	public function index()
	{
		$this->pyrocache->delete_all('modules_m');
		// Create pagination links
		$total_rows = $this->cip_servers_m->count_all();
		$pagination = create_pagination('admin/cirpho/servers/index', $total_rows, NULL, 5);
		// Using this data, get the relevant results
		$servers = $this->cip_servers_m->order_by('id')->limit($pagination['limit'])->get_all();
		$this->template
			->title($this->module_details['name'], lang('cirpho_category_list_title'))
			->set('servers', $servers)
			->set('pagination', $pagination)
			->build('admin/servers/index', $this->data);
	}
	/**
	 * Create method, creates a new category
	 * @access public
	 * @return void
	 */
	public function create()
	{
		
		// Validate the data
		if ($this->form_validation->run())
		{
			$this->cip_servers_m->insert($_POST)
				? $this->session->set_flashdata('success', sprintf( lang('cirpho_server_add_success'), $this->input->post('name')) )
				: $this->session->set_flashdata(array('error'=> lang('cirpho_server_add_error')));
			redirect('admin/cirpho/servers');
		}
		
		// Loop through each validation rule
		foreach($this->validation_rules as $rule)
		{
			$server->{$rule['field']} = set_value($rule['field']);
		}
		// Render the view	
		$this->data->server =& $server;	
		$this->template->title($this->module_details['name'], lang('cirpho_server_create_title'))
						->build('admin/servers/form', $this->data);	
	}
	
	/**
	 * Edit method, edits an existing category
	 * @access public
	 * @param int id The ID of the category to edit 
	 * @return void
	 */
	public function edit($id = 0)
	{	
		// Get the category
		$server = $this->cip_servers_m->get($id);
		
		// ID specified?
		$server or redirect('admin/cirpho/servers/index');
		
		// Validate the results
		if ($this->form_validation->run())
		{		
			$this->cip_servers_m->update($id, $_POST)
				? $this->session->set_flashdata('success', sprintf( lang('cirpho_server_edit_success'), $this->input->post('name')) )
				: $this->session->set_flashdata(array('error'=> lang('cirpho_server_edit_error')));
			
			redirect('admin/cirpho/servers/index');
		}
		
		// Loop through each rule
		foreach($this->validation_rules as $rule)
		{
			if($this->input->post($rule['field']) !== FALSE)
			{
				$server->{$rule['field']} = $this->input->post($rule['field']);
			}
		}

		// Render the view
		$this->data->server =& $server;
		$this->template->title($this->module_details['name'], sprintf(lang('cirpho_server_edit_title'), $server->name))
						->build('admin/servers/form', $this->data);
	}	

	/**
	 * Delete method, deletes an existing category (obvious isn't it?)
	 * @access public
	 * @param int id The ID of the category to edit 
	 * @return void
	 */
	public function delete($id = 0)
	{	
		$id_array = (!empty($id)) ? array($id) : $this->input->post('action_to');
		
		// Delete multiple
		if(!empty($id_array))
		{
			$deleted = 0;
			$to_delete = 0;
			foreach ($id_array as $id) 
			{
				if($this->cip_servers_m->delete($id))
				{
					$deleted++;
				}
				else
				{
					$this->session->set_flashdata('error', sprintf($this->lang->line('cirpho_server_mass_delete_error'), $id));
				}
				$to_delete++;
			}
			
			if( $deleted > 0 )
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('cirpho_server_mass_delete_success'), $deleted, $to_delete));
			}
		}		
		else
		{
			$this->session->set_flashdata('error', $this->lang->line('cirpho_server_no_select_error'));
		}
		
		redirect('admin/cirpho/servers/index');
	}
	
	/**
	 * Callback method that checks the title of the category
	 * @access public
	 * @param string title The title to check
	 * @return bool
	 */
	public function _check_title($name = '')
	{
		if ($this->cip_servers_m->check_title($name))
		{
			$this->form_validation->set_message('_check_title', sprintf($this->lang->line('cirpho_server_already_exist_error'), $name));
			return FALSE;
		}

		return TRUE;
	}
}